#include "led.h"
#include "work.h"
#include "temp.h"
#include "btim.h"

extern uint8_t Work_Mode,Work_Strength,Current_Temp;
extern uint8_t Charge_State,ems_pulse,Beep_Time;
extern uint16_t VDD_Voltage;
uint8_t power_cnt=0,flash_cnt=0,flash_cnt1=0,lv_cnt,lv_cnt1=0;
extern boolean_t Touch_OK,Fan_Error,Temp_Error,life_mode,age_mode;
extern boolean_t lvp_f, beep_f, uvp_f, load_f;
boolean_t flash_f,flash_f1,flash_f2=0,LED_F=0;
extern uint8_t bat_low_flag;

uint8_t LED_BREATH=0,br_cnt;
uint8_t led_data[24]={};
uint8_t led_data1[24]={};
	
void LED_Handle()
{
		uint8_t i;
		// static uint8_t print_delay;

		flash_cnt++;
		if(flash_cnt == 40){
			flash_f1^=1;
		}
		
		if(flash_cnt > 79){
			flash_cnt = 0;
			flash_f1^=1;
			flash_f^=1;
		}
		for(i = 0; i < 24; i++)
		{
			led_data[i] = 0;
			led_data1[i] = 0;
		}

		if(Work_Mode){
			if(bat_low_flag){
				led_data[Work_Mode*3-2]=0;
				if(flash_f1){
					for(i = 0; i <= 7; i++){
						led_data[12+i/2*3] = LED_BRIGHT;
					}
				}
				else{
					for(i = 0; i <= 7; i++){
						led_data[12+i/2*3] = 0;
					}
				}
			}
			else{
				led_data[Work_Mode*3-2]=GREEN_LED_BRIGHT;

				// if(load_f == 0){		// debug 时开启，方便测试带载情况
				// 	led_data[Work_Mode*3-1]=LED_BRIGHT;
				// }
			

				if(lvp_f == 1)	// 电池电压低于3.5v 时显示
				{
					if(uvp_f)	//red  电池电压低于3.4v时显示
					{
						for(i=0 ;i<=7;i++)
						{
							led_data[12+i/2*3]=LED_BRIGHT;
						}
					}
					else
					{
						if(flash_cnt1<5)	// flick_5 times
						{
							for(i = 0; i <= 7; i++){
								led_data[12+i/2*3] = LED_BRIGHT;
							}
							if(flash_f){
								for(i = 0; i <= 7; i++){
									led_data[12+i/2*3] = 0;
								}
								if(flash_f2 == 1){
									flash_f2 = 0;
									if(flash_cnt1 < 5){
										flash_cnt1++;
									}
								}
							}
							else{
								flash_f2=1;
							}
						}
						else	//ͬ同绿色档位显示 
						{
							for(i = 0; i <= Work_Strength; i++){
									led_data[13+i/2*3]=GREEN_LED_BRIGHT;
							}
							if(flash_f){
								if(Work_Strength%2==0){
									led_data[13+Work_Strength/2*3]=0;
								}
							}
						}
					}
			
					lv_cnt++;
					if(lv_cnt > 199){
						lv_cnt=0;
						//1min power_off
						if((lv_cnt1 == 30)&&(uvp_f == 1)){
							Work_Mode=0;
							Beep_Time=3;
						}

						lv_cnt1++;
						//149=5min 
						if(lv_cnt1 > 149){
							lv_cnt1 = 0;
							flash_cnt1 = 0;
							flash_cnt = 0;
							flash_f = 0;
						}
					}
				}
				else
				{
					flash_cnt1 = 0;
					lv_cnt1 = 0;
					lv_cnt = 0;
					for(i = 0; i <= Work_Strength; i++){
						led_data[13+i/2*3] = GREEN_LED_BRIGHT;
					}
					if(flash_f){
						if(Work_Strength%2 == 0){
							led_data[13+Work_Strength/2*3] = 0;
						}	
					}
				}
			}
		}
		else
		{
			flash_cnt1 = 0;
			lv_cnt1  =0;
			lv_cnt = 0;
			// 充电中,[档位灯---红色 呼吸]
			if(Charge_State == 1)
			{
				for(i = 0; i <= 7; i++){
					led_data[12+i/2*3] = LED_BREATH;
				}
				br_cnt++;
				if(br_cnt > 7*2){
					br_cnt = 0;
					if(LED_F == 0){
						LED_BREATH++;
						if(LED_BREATH >= LED_BRIGHT){
							LED_F = 1;
						}
					}
					else{
						if(LED_BREATH>0){
							LED_BREATH--;
						}
						else{
							LED_F = 0;
						}	
					}
				}
			}
			// 充满，[档位灯---绿色 常亮]
			else if(Charge_State == 2){
				for(i = 0; i <= 7; i++){
					led_data[13+i/2*3]=GREEN_LED_BRIGHT;
				}
			}
			// 未充电
			else{
				LED_BREATH = 0;
				LED_F = 0;
			}
		}
		for(i = 0; i < 12; i++)
		{
			led_data1[i] = led_data[i + 12];
			led_data1[12+i] = led_data[21 + i%3*2-12-i];
		}
//		print_delay ++;
		if((beep_f == 0)&&(ems_pulse == 0)){

			WS2812_SendData(led_data1, 8);

			// }
		}
		
}

void delay(uint8_t us)
{
		uint8_t i;
		for(i=0;i<us;i++)
		__NOP();
}

/**************************************
 * ws2812 驱动逻辑电平: 先高电平，后低电平
 * 0码： H(200ns~380ns)+L(580ns ~1us)
 * 1码： H(580ns~1us) +L(580ns~1us)
 * 
*******************************************/
void WS2812_SendByte(uint8_t byte) //0.4ms
{
    for (int i = 7; i >= 0; i--) {
        if (byte & (1 << i)) {
            // ����1: �ߵ�ƽ 0.8us���͵�ƽ 0.4us
            DI_ON;
            delay(2);  // 1us��ʱ
            DI_OFF;
            delay(1);  // 3us��ʱ
        } else {
            // ����0: �ߵ�ƽ 0.4us���͵�ƽ 0.8us
            DI_ON;
			__NOP();
			__NOP();
			__NOP();
            DI_OFF;
            delay(2);  // 2us��ʱ
        }
    }
}

/**************************************
 *  send rgb data
 * ************************************/
void WS2812_SendLED(uint8_t r, uint8_t g, uint8_t b) 
{
    WS2812_SendByte(g);  // WS2812����˳����G-R-B
    WS2812_SendByte(r);
    WS2812_SendByte(b);
}

// ���Ͷ��LED���ݣ�����ģʽ��
void WS2812_SendData(uint8_t* data, uint16_t length) 
{
	Btim_DisableIT(BTIM3, BTIM_IT_UI);      //����BTIM3����ж�    
    EnableNvic(BTIM3_4_5_IRQn, IrqLevel0, FALSE);
    for (int i = 0; i < length; i++) {
        uint8_t r = data[i * 3];
        uint8_t g = data[i * 3 + 1];
        uint8_t b = data[i * 3 + 2];
        WS2812_SendLED(r, g, b);
    }
	Btim_EnableIT(BTIM3, BTIM_IT_UI);      //����BTIM3����ж�    
	EnableNvic(BTIM3_4_5_IRQn, IrqLevel0, TRUE);
}
